import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Backend as B
import Gui.Global as G
import Gui.Components

Item {
    readonly property int viewId: G.Enums.ViewId.Search
    B.SearchPageViewModel {
        id: viewModel

        onLoadingSearchResult: function (loading) {
            if (loading) {
                showLoading();
            } else {
                if (viewModel.musicListModel.count === 0) {
                    showEmpty();
                } else {
                    showResult();
                }
            }
        }
    }

    Component {
        id: infoComponent
        Item {
            property alias info: infoItem.text
            anchors.fill: parent
            Label {
                id: infoItem
                anchors.centerIn: parent
                font.pixelSize: 42
            }
        }
    }

    Component {
        id: logoComponent

        FontIcon {
            anchors.fill: parent
            iconName: "ri.netease-cloud-music-fill"
            iconSize: 100
        }
    }
    Component {
        id: resultComponent

        MusicListView {
            anchors.fill: parent
            model: viewModel.musicListModel

            loadingMore: viewModel.loadingMore
            loadingAllFinished: viewModel.loadingAllFinished

            // 连接信号
            onPlayMusic: function (index) {
                viewModel.playMusic(index);
            }
            onAddMusic: function (index) {
                viewModel.addMusic(index);
            }
            onDownloadMusic: function (index) {
                viewModel.downloadMusic(index);
            }
            onLoadMore: function () {
                viewModel.loadMore();
            }
        }
    }

    function showInfo(info) {
        contentLoader.sourceComponent = infoComponent;
        contentLoader.item.info = info;
    }
    function showError() {
        showInfo("加载失败");
    }
    function showEmpty() {
        showInfo("没有找到相关内容");
    }
    function showLoading() {
        showInfo("正在加载中");
    }
    function showLogo() {
        contentLoader.sourceComponent = logoComponent;
    }
    function showResult() {
        contentLoader.sourceComponent = resultComponent;
    }

    Loader {
        id: contentLoader
        anchors.fill: parent
        sourceComponent: logoComponent
    }

    Connections {
        target: G.SignalBus

        function onSearch(keyword) {
            viewModel.search(keyword);
        }
    }
}
